package com.cwd.arm.application.service.read;

import com.cwd.arm.application.dto.MonthAmountTotalDto;
import com.cwd.arm.application.dto.board.YearAmountDto;
import com.cwd.arm.application.service.read.facade.MonthAmountTotalReadService;
import com.cwd.arm.infrastructure.dao.MonthAmountTotalDao;
import com.cwd.arm.infrastructure.po.MonthAmountTotalPo;
import com.cwd.arm.infrastructure.po.YearAmountPo;
import com.cwd.arm.infrastructure.query.MonthAmountTotalDaoQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
* Description:每月金额统计
* Created on 2021年11月16日
* @author  yan
*/
@Repository
@Primary
public class MonthAmountTotalReadServiceImpl implements MonthAmountTotalReadService {

    @Autowired
    private MonthAmountTotalDao monthAmountTotalDao;

    @Override
    public List<MonthAmountTotalDto> selectByYear(int year) {
        MonthAmountTotalDaoQuery daoQuery = new MonthAmountTotalDaoQuery();
        daoQuery.setYear(year);
        return selectByQuery(daoQuery);
    }

    @Override
    public List<MonthAmountTotalDto> selectByQuery(MonthAmountTotalDaoQuery daoQuery) {
        List<MonthAmountTotalPo> poList = monthAmountTotalDao.selectQuery(daoQuery);
        return convertToMonthAmountTotalDtoList(poList);
    }

    @Override
    public List<YearAmountDto> getYearConfirmAmount() {
        List<YearAmountPo> yearAmountPoList = monthAmountTotalDao.selectYearConfirmIncome();
        return convertToYearAmountDtoList(yearAmountPoList);
    }

    private MonthAmountTotalDto convertToDto(MonthAmountTotalPo po){
        MonthAmountTotalDto dto = new MonthAmountTotalDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    private List<MonthAmountTotalDto> convertToMonthAmountTotalDtoList(List<MonthAmountTotalPo> poList){
        return poList.stream().map(po->(convertToDto(po))).collect(Collectors.toList());
    }

    private YearAmountDto convertToDto(YearAmountPo po){
        YearAmountDto dto = new YearAmountDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    private List<YearAmountDto> convertToYearAmountDtoList(List<YearAmountPo> poList){
        return poList.stream().map(po->(convertToDto(po))).collect(Collectors.toList());
    }

}
